package com.example.administrator.fishbone.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.administrator.fishbone.bean.OnClickBack;
import com.example.administrator.fishbone.bean.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${杨文超} on 2016/8/30.
 */
public abstract class CommonAdapter<T> extends BaseAdapter implements OnClickBack{
    List<T> lists = new ArrayList<>();
    LayoutInflater inflater;
    int mLayoutId;

    public CommonAdapter(Context context, List<T> list, int mLayoutId) {
        if (list != null) {
        lists .addAll(list);

        }
        this.mLayoutId = mLayoutId;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public T getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(mLayoutId, parent, false);
            holder = new ViewHolder(convertView,this);
            setListener(position, holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setPosition(position);
        setViewData(getItem(position),holder);
        return convertView;
    }
    @Override
    public void onClickBack(int position, View view, ViewHolder holder) { //点击事件上抛
        onClick(position, view, holder);
    }

    public abstract void setListener(int position, ViewHolder holder);

    public abstract void onClick(int position, View convertView, ViewHolder viewHolder);
    public abstract void setViewData(T item, ViewHolder holder);

}
